fix: RoadRunner streaming through generators #939
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #903.
RoadRunner supports streamed responses, but not in the same way that other servers do. Instead out using the output buffer with
echo
, it supports using aGenerator
function as the response callback to "yield" each chunk of the response.Previous fixes have involved collecting the output buffer and returning it at once from the server. This is not streaming as all chunks are collected into memory and sent at once.
This implementation allows you to use a
Generator
as the callable in the streamed response. Eachyield
sends a chunk in the response. It is quite simple and just interacts with the underlyingHttpWorker
that is used by the Roadrunner client. The new code is only invoked if the streamed response function is a generator, else the old behaviour still persists.Note: we do not need to evaluate the function to know if we need to stream it in this way, we use reflection to do this ahead of time, ensuring that the function returns a
Generator
object.I would like to thank @donnysim for his investigative work and initial solution which was adapted into this PR.